package watermark;

import main.Main;

import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

/**
 * 添加图片的主窗体
 * @author syt
 */
public class WaterMark extends JFrame {

    private static final WaterMark waterMark = new WaterMark();
    public static WaterMark getInstance() {
        return waterMark;
    }
    public static void mainWaterMark() {
        waterMark.setVisible(true);
    }

    private String srcImgPath = new String();
    private JLabel label;

    /*
     * 创建按钮，菜单组件
     */
    private JToolBar toolBar;
    private JButton insertPictureButton;
    private JButton insertImageMarkButton;
    private JButton insertTextMarkButton;
    private JButton returnButton;

    public WaterMark() {
        setResizable(false);
        setTitle("画图程序(添加水印)");
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();     //得到屏幕的尺寸
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); //点击关闭按钮时关闭程序
        setVisible(true); //显示窗体
        setSize(screenSize.width,screenSize.height - 10);
        init();
        addListener();
    }
    /**
     * 初始化窗体页面
     */
    private void init() {
        toolBar = new JToolBar();
        getContentPane().add(toolBar, BorderLayout.NORTH);//将工具栏放在窗体上边

        /*
         * 设置各组件位置
         */
        insertPictureButton = new JButton("插入图片");
        insertPictureButton.setToolTipText("插入图片");
        toolBar.add(insertPictureButton);
        toolBar.addSeparator();

        insertImageMarkButton = new JButton("插入图片水印");
        insertImageMarkButton.setToolTipText("插入图片水印");
        toolBar.add(insertImageMarkButton);
        toolBar.addSeparator();

        insertTextMarkButton = new JButton("插入文字水印");
        insertTextMarkButton.setToolTipText("插入文字水印");
        toolBar.add(insertTextMarkButton);
        toolBar.addSeparator();

        returnButton = new JButton("返回");
        returnButton.setToolTipText("返回");
        toolBar.add(returnButton);
        toolBar.addSeparator();

        label = new JLabel();
        getContentPane().add(label);
    }

    /**
     * 监听器
     */
    private void addListener() {
        insertPictureButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //读取图片
                JFileChooser fileChooser = new JFileChooser();
                fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
                FileNameExtensionFilter filter = new FileNameExtensionFilter(
                        "图片文件(*.bmp, *.gif, *.jpg, *.jpeg, *.png)", "bmp",
                        "gif", "jpg", "jpeg", "png");
                fileChooser.setFileFilter(filter);
                fileChooser.setDialogTitle("选择图片文件");
                int result = fileChooser.showDialog(null, "确定");
                if (result == JFileChooser.APPROVE_OPTION) {
                    srcImgPath = fileChooser.getSelectedFile().getPath();
                }
                BufferedImage img = null;
                try {
                    img = ImageIO.read(new File(srcImgPath));
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
                getContentPane().setLayout(null);
                //设置图片
                label.setIcon(new ImageIcon(img));
                //将图片放到窗体中间
                int midX = getContentPane().getWidth() / 2 - img.getWidth() / 2;
                int midY = getContentPane().getHeight() / 2 - img.getHeight() / 2;
                label.setBounds(midX, midY, img.getWidth(), img.getHeight());
            }
        });

        insertImageMarkButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (srcImgPath.isEmpty()){
                    JOptionPane.showMessageDialog(null, "请先插入图片");
                }
                else if (MainPictureFrame.getInstance() == null){
                    MainPictureFrame.getInstance();
                }
                else {
                    MainPictureFrame.getInstance().show();
                    if (!srcImgPath.equals(MainPictureFrame.getInstance().getFilepath())){
                        MainPictureFrame.getInstance().getFilepathTF().setText(srcImgPath);
                        MainPictureFrame.getInstance().getSavepathTF().setText(
                                srcImgPath.substring(0,srcImgPath.lastIndexOf("\\")) + Common.getNewFileorDirName(srcImgPath));
                    }
                }
            }
        });

        insertTextMarkButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (srcImgPath.isEmpty()){
                    JOptionPane.showMessageDialog(null, "请先插入图片");
                }
                else if (MainFrame.getInstance() == null){
                    MainFrame.getInstance();
                }
                else {
                    MainFrame.getInstance().show();
                    if (!srcImgPath.equals(MainFrame.getInstance().getFilepath())){
                        MainFrame.getInstance().getFilepathTF().setText(srcImgPath);
                        MainFrame.getInstance().getSavepathTF().setText(
                                srcImgPath.substring(0,srcImgPath.lastIndexOf("\\")) + Common.getNewFileorDirName(srcImgPath));
                    }
                }
            }
        });

        returnButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                dispose();
                Main.returnFunc();
            }
        });
    }

    /**
     * 获取要处理的图片路径
     * @return 图片路径
     */
    public String getSrcImgPath() {
        return srcImgPath;
    }
    /**
     * 获取放置图片的label组件
     * @return JLabel
     */
    public JLabel getLabel() {
        return label;
    }
}

